import config
from ecs import AliEcsClient
from server import ServerManager
import sys


if __name__ == '__main__':

    conf = config.load_config()
    if conf is None:
        print 'please check config.json'
        exit(1)

    server_manager = ServerManager(AliEcsClient(conf.ecs_config), conf.server_config)

    if len(sys.argv) > 1:
        s = str(sys.argv[1])
        if s == '1':
            server_manager.create_alloc_start_install()
        elif s == '2':
            server_manager.stop_delete_all()
        elif s == '3':
            server_manager.show_all()
        elif s == '4':
            config.load_config()
        else:
            print 'input error'
        exit(0)

    while True:
        print ''
        print '-------MAIN MENU-------'
        print '[1] --- auto install'
        print '[2] --- auto uninstall all'
        print '[3] --- show all instances'
        print '[4] --- reload config file'
        print '[0] --- exit'
        s = raw_input('Enter your input: ')
        print ''
        if s == '1':
            server_manager.create_alloc_start_install()
        elif s == '2':
            server_manager.stop_delete_all()
        elif s == '3':
            server_manager.show_all()
        elif s == '4':
            conf = config.load_config()
            if conf is None:
                print 'please check config.json'
                exit(1)

            server_manager = ServerManager(AliEcsClient(conf.ecs_config), conf.server_config)
        elif s == '6':
            pass
        elif s == '0':
            exit(0)
        else:
            print 'input error'
